<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>上传文件</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
   <script src="plug-in/jquery-plugs/form/jquery.form.js"></script>
  <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.config.js"></script>
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.all.min.js"> </script> 
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/lang/zh-cn/zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_v5.3.1_min_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_Datatype_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/datatype_zh-cn.js"></script>
   <link rel="stylesheet" href="plug-in/Validform/css/style.css" type="text/css">
   <link rel="stylesheet" href="plug-in/Validform/css/tablefrom.css" type="text/css">
   
   <!-- 上传文件  -->
    <link rel="stylesheet" type="text/css" href="plug-in/webUploader/css/webuploader.css" />
    <link rel="stylesheet" type="text/css" href="plug-in/webUploader/style.css" />
    <script type="text/javascript" src="plug-in/webUploader/dist/webuploader.js"></script>
   
  	<style>
  		.prevImg{
  			float:left;
  			margin:2px;
  		}
  		.prevImg a{
  			display: block;
    		text-align: center;
    		cursor:pointer;
    		color:rgb(0,162,212)
  		}
  		.split{
		    background: white;
		    height: 30px;
		    line-height: 30px;
		    color: #8f0911;
		    font-weight: bold;
		    padding-left: 15px;
  		}
  	</style>
 </head>
 <body>
 <div class="split">上传视频</div>
  <form id="vedioForm" title="上传视频" action="teachItemPreviewController.do?doAdd" name="vedioForm" method="post">
		<input type="hidden" name="fileType" value="0"/>
		<input type="hidden" name="teachItemId" value="${teachItemId }"/>
		<table style="" cellpadding="0" cellspacing="1" class="formtable">
			<tbody>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件名:
						</label>
					</td>
					<td class="value">
					    <input onkeypress="specialTextValidate()" id="name" name="name" type="text" 
					     	 style="width: 150px" class="inputxt" datatype="*">
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">文件名</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							是否必学:
						</label>
					</td>
					<td class="value">
					     	<input type="radio" name="isMustLearn" value="0" checked="checked"/>否
				     	 	<input type="radio" name="isMustLearn" value="1"/>是
							<span class="Validform_checktip"></span>
							<label class="Validform_label" style="display: none;">设备品牌</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件:
						</label>
					</td> 
					<td class="value"> 
						<input name="file"  accept=".xls,.xlsx" type="file"/>
						<button type="button" onclick="uploadFile('vedioForm')">上传</button>
		           </td> 
			  </tr>
			  <tr>
				<td class="value" colspan="2" style="text-align: center;"> 
					<button type="button" onclick="save('vedioForm')">保存</button>
				</td>
			  </tr>
			</tbody>
		</table>
		
		</form>
		
	<div class="split">上传文档</div>
  <form id="documentForm" enctype="multipart/form-data" action="teachItemPreviewController.do?doAdd" name="formobj" method="post">
		<input type="hidden" name="fileType" value="1"/>
		<input type="hidden" name="teachItemId" value="${teachItemId }"/>
		<table style="" cellpadding="0" cellspacing="1" class="formtable">
			<tbody>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件名:
						</label>
					</td>
					<td class="value">
					    <input onkeypress="specialTextValidate()" id="name" name="name" type="text" 
					     	 style="width: 150px" class="inputxt" value="${obj.name}" datatype="*">
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">文件名</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							是否必学:
						</label>
					</td>
					<td class="value">
					     	<input type="radio" name="isMustLearn" value="0" checked="checked"/>否
				     	 	<input type="radio" name="isMustLearn" value="1"/>是
							<span class="Validform_checktip"></span>
							<label class="Validform_label" style="display: none;">设备品牌</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件:
						</label>
					</td> 
					<td class="value"> 
						<input name="file" type="file" accept=".xls,.xlsx"/>
						<button type="button" onclick="uploadFile('documentForm')">上传</button>
		           </td> 
			  </tr>
			  <tr>
				<td class="value" colspan="2" style="text-align: center;"> 
					<button type="button" onclick="save('documentForm')">保存</button>
				</td>
			  </tr>
			</tbody>
		</table>
		</form>
		
		<t:datagrid name="list" checkbox="false" fitColumns="true"
	  	 title="" actionUrl="teachItemPreviewController.do?datagrid&teachItemId=${teachItemId }" 
	  	 idField="id" fit="true" queryMode="group">
		   <t:dgCol title="主键"  field="id"  hidden="true"  queryMode="single" width="100"></t:dgCol>
		   <t:dgCol title="文件名"  field="name"  hidden="false" query="false"  queryMode="single" width="100"></t:dgCol>
		   <t:dgCol title="文件类型"  field="fileType"  hidden="false" query="false" queryMode="single" formatterjs="function(v,rec){return v==0? '视频':'文档'}" width="100"></t:dgCol>
		   <t:dgCol title="是否必学"  field="isMustLearn"  hidden="false"  queryMode="single" query="false" formatterjs="function(v,rec){return v==0? '否':'是'}" width="100"></t:dgCol>
		   <t:dgCol title="操作" field="opt" width="100"></t:dgCol>
		   <t:dgFunOpt funname="downloadFile(id)" title="下载"></t:dgFunOpt>
		   <t:dgFunOpt funname="editRow(id)" title="编辑"></t:dgFunOpt>
		   <t:dgFunOpt funname="deleteRow(id)" title="删除"></t:dgFunOpt>
	  	</t:datagrid>
	  	
		<script>
			var vedioForm = $('#vedioForm').Validform({tiptype:"3"}); //初始化校验form控件
			var documentForm = $('#documentForm').Validform({tiptype:"3"}); //初始化校验form控件
			 $(document).ready(function () {
				
				//判断是否是查看页面,不是查看页面，初始化编辑器
				if(location.href.indexOf("load=detail")==-1){
					UE.getEditor("detailEditor",{});//初始化editor控件
				}else{
					UE.getEditor("detailEditor",{
						readonly:true, 
						toolbars: []
					});//初始化editor控件
				}
			});
			
			function downloadFile(id){
				$.ajax({
					url: 'cms-attach.do?hasFile',
					dataType: 'json',
					data: {'ownerId':id},
					success: function(data){
						if(data.success){
							window.location.href="cms-attach.do?download&ownerId="+id;
						}else{
							tip(data.msg);
							return;
						}
					}
				});
		 	}
			 
			function save(formId){
				if(formId=='documentForm'){
					var valid = documentForm.check(false);
					if(!valid) return;
				}
				if(formId=='vedioForm'){
					var valid = vedioForm.check(false);
					if(!valid) return;
				}
				
				$.ajax({
					url: ''+$("#"+formId).attr('action'),
					dataType: 'json',
					data: $("#"+formId).serialize(),
					type: 'post',
					success: function(data){
						if(data.success){
							window.location.reload();
						}else{
							tip(data.msg);
						}
					},
				});
			}
			
			function deleteAttach(id){
				 $('#prevImg'+id).remove();
				 $('#formobj').append("<input type='hidden' name='deletedAttach'  value='"+id+"'/>");
			}
		 
			function deleteRow(id){
				$.ajax({
					url: 'teachItemPreviewController.do?doDel',
					dataType: 'json',
					data: {'id':id},
					success: function(data){
						if(data.success){
							tip("删除成功");
							$('#list').datagrid('reload');
						}else{
							tip(data.msg);
						}
					}
				});
			}
			
			function editRow(id){
				$.dialog({
				  	id:'xxx',
					content: 'url:teachItemPreviewController.do?goUpdate&id='+id,
					lock : true,
					zIndex: getzIndex(), 
					parent:frameElement.api,
					width:480,
					height:350,
					title:'上传实验报告模板', 
					opacity : 0.3,
					cache:false,
				    ok: function(){
				    	iframe = this.iframe.contentWindow;
			    	 	$('#btn_sub', iframe.document).click(); 
				    	 return false;
				    },
				    cancelVal: '关闭',
				    cancel: true /*为true等价于function(){}*/
				});
			}
			
			function uploadFile(formId){
				if(typeof($('#'+ formId +' [name="file"]')[0].files[0]) == 'undefined'){
					tip('没有选择文件');
					return;
				}	
				var formData = new FormData();
				formData.append('file', $('#'+ formId +' [name="file"]')[0].files[0]);
				$.ajax({
				    url: 'cms-attach.do?upload',
				    type: 'POST',
				    cache: false,
				    data: formData,
				    dataType: 'json',
				    processData: false,
				    contentType: false,
				    success: function(data){
						if(data.success){
							$("#"+formId).append('<input type="hidden" name="attachId" value="'+ data.msg +'">');
							tip('上传成功');
							return;
						}else{
							tip(data.msg);
						}
					}
				});
			}
		</script>
 </body>
 